package com.zr.subclass.service.impl;

import com.zr.planclass.pojo.PlanClassVo;
import com.zr.subclass.mapper.SubClassMapper;
import com.zr.subclass.meiju.StatusEnumSub;
import com.zr.subclass.pojo.SubClassAddVo;
import com.zr.subclass.pojo.SubClassSelectVo;
import com.zr.subclass.pojo.SubClassUpdateVo;
import com.zr.subclass.pojo.SubClassVo;
import com.zr.subclass.pojo.SubClassUpdateStatusVo;
import com.zr.subclass.service.SubClassService;
import com.zr.util.AllRecords;
import com.zr.util.ConfigErrorMessage;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Liyiding on 2019/9/22.
 */
@Service
public class SubClassServiceImpl implements SubClassService {

    @Autowired
    private SubClassMapper subClassMapper;
    @Override
    public ResultVo queryPage(SubClassSelectVo subClassSelectVo) {
        //根据条件查询数量
        int count =  subClassMapper.queryCount(subClassSelectVo);
        //根据条件查询数据
        List<SubClassVo> subClassVos = subClassMapper.queryData(subClassSelectVo);
        //把0，1转换成汉字
        for (SubClassVo subClassVo:subClassVos){
            System.out.println(StatusEnumSub.getName(subClassVo.getStatus()));
            subClassVo.setStatusName(StatusEnumSub.getName(subClassVo.getStatus()));
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(subClassSelectVo.getPageIndex());
        allRecords.setPageSize(subClassSelectVo.getPageSize());
        allRecords.resetTotalPage(count);
        allRecords.setTotalNumber(count);
        allRecords.setDataList(subClassVos);
        return ResultVoBuilder.success(allRecords);
    }


    @Override
    @Transactional
    public ResultVo add(@Valid SubClassAddVo subClassAddVo) {
        //验证数据
        ResultVo assigement = assigement(subClassAddVo);
        if (!assigement.getSuccess()){
            return assigement;
        }
        //给创建人和创建时间等参数赋值
        SubClassVo subClassVo = new SubClassVo();
        //把左边对象中的参数值自动赋值给右边对象中的参数值
        //注意必须是相同类型和相同参数名的才能copy
        BeanUtils.copyProperties(subClassAddVo,subClassVo);
        Date nowDate = new Date();
        subClassVo.setCreateName("小明");
        subClassVo.setCreateTime(nowDate);
        subClassVo.setUpdateName("小明");
        subClassVo.setUpdateTime(nowDate);
        subClassMapper.add(subClassVo);
        return ResultVoBuilder.success();
    }


    public ResultVo assigement(SubClassAddVo subClassAddVo){
        //验证数据库中是否存在名称
        List<SubClassVo> subClassNames = subClassMapper.queryByName(subClassAddVo.getName());
        //CollectionUtils验证集合是否为空
        if (!CollectionUtils.isEmpty(subClassNames)){
            //如果不为空把错误提示给前端展示
            return ResultVoBuilder.error("500", ConfigErrorMessage.YIJINGCUNZAI);
        }
        //验证是否存在编码
        List<SubClassVo> subClassCodes =subClassMapper.queryByCode(subClassAddVo.getCode());
        //CollectionUtils验证集合是否为空
        if (!CollectionUtils.isEmpty(subClassCodes)){
            //如果不为空把错误提示给前端展示
            return ResultVoBuilder.error("500",ConfigErrorMessage.CUNZAIBIANMA);
        }
        return ResultVoBuilder.success();
    }

    @Override
    @Transactional
    public ResultVo update(@Valid SubClassUpdateVo subClassUpdateVo) {
        //验证数据
        ResultVo assigementUpdate = assigementUpdate(subClassUpdateVo);
        if (!assigementUpdate.getSuccess()){
            return assigementUpdate;
        }
        //验证是否存在要修改的大类
        SubClassVo subClassVo=subClassMapper.queryById(subClassUpdateVo.getId());
        if(subClassVo==null){
            return  ResultVoBuilder.error("500",ConfigErrorMessage.QUERYBYID);
        }
        //验证是否为禁用状态
        if(subClassVo.getStatus()==StatusEnumSub.QIYONG.getStatusValue()){
            return  ResultVoBuilder.error("500",ConfigErrorMessage.ZHUANGTAI);
        }
        //验证名字是否存在
        List<SubClassVo> list=subClassMapper.queryByName(subClassUpdateVo.getName());
        if(!CollectionUtils.isEmpty(list)){
            //根据id判断是否修改的数据和查询出数据的id一致
            // 如果一致代表修改的还是原来的名称不用提示错误
            //否则提示名称已经存在！
            if (list.get(0).getId()!=subClassUpdateVo.getId()){
                return ResultVoBuilder.error("500",ConfigErrorMessage.YIJINGCUNZAI);
            }
        }
        //给修改人和修改时间等参数赋值
        SubClassVo subClassVo1 = new SubClassVo();
        //把左边对象中的参数值自动赋值给右边对象中的参数值
        //注意必须是相同类型和相同参数名的才能copy
        BeanUtils.copyProperties(subClassUpdateVo,subClassVo1);
        Date nowDate = new Date();
        subClassVo1.setUpdateName("小明");
        subClassVo1.setUpdateTime(nowDate);
        int update = subClassMapper.update(subClassVo1);
        if (update==0){
            return ResultVoBuilder.error("500","请刷新后重新修改！");
        }
        return ResultVoBuilder.success();
    }

    public ResultVo assigementUpdate(SubClassUpdateVo subClassUpdateVo){
        //验证数据库中是否存在名称
        List<SubClassVo> subClassNames = subClassMapper.queryByName(subClassUpdateVo.getName());
        //CollectionUtils验证集合是否为空
        if (!CollectionUtils.isEmpty(subClassNames)){
            //如果不为空把错误提示给前端展示
            return ResultVoBuilder.error("500", ConfigErrorMessage.YIJINGCUNZAI);
        }
        return ResultVoBuilder.success();
    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid SubClassUpdateStatusVo subClassUpdateStatusVo) {
        //验证要修改的数据是否存在
        SubClassVo subClassVo = subClassMapper.queryById(subClassUpdateStatusVo.getId());
        if (subClassVo==null){
            return ResultVoBuilder.error("500","修改的数据不存在！");
        }
        //给修改人员和修改时间赋值
        SubClassVo subClassVo1=new SubClassVo();
        BeanUtils.copyProperties(subClassUpdateStatusVo,subClassVo1);
        subClassVo1.setUpdateName("小明");
        subClassVo1.setUpdateTime(new Date());
        subClassMapper.updateStatus(subClassVo1);
        return ResultVoBuilder.success();
    }
}
